package com.zzz.robot201.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.zzz.robot201.R;
import com.zzz.robot201.di.component.DaggerMsgFeedBackOneComponent;
import com.zzz.robot201.mvp.contract.MsgFeedBackOneContract;
import com.zzz.robot201.mvp.presenter.MsgFeedBackOnePresenter;

import java.util.Timer;
import java.util.TimerTask;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;
import static com.zzz.robot201.mvp.ui.activity.HomeActivity.responseMsg;

/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 07/10/2020 02:56
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class MsgFeedBackOneFragment extends BaseFragment<MsgFeedBackOnePresenter> implements MsgFeedBackOneContract.View {

    @BindView(R.id.tv_xzz)
    TextView tvXzz;
    @BindView(R.id.tv_phjd)
    TextView tvPhjd;
    @BindView(R.id.tv_cqjd)
    TextView tvCqjd;
    @BindView(R.id.tv_dl)
    Button tvDl;
    @BindView(R.id.tv_gh)
    TextView tvGh;
    @BindView(R.id.tv_dqbt)
    TextView tvDqbt;
    @BindView(R.id.tv_dqbtzt)
    TextView tvDqbtzt;
    @BindView(R.id.tv_kzms)
    TextView tvKzms;
    @BindView(R.id.tv_mbd_x)
    TextView tvMbdX;
    @BindView(R.id.tv_mbd_y)
    TextView tvMbdY;
    @BindView(R.id.tv_ry_x)
    TextView tvRyX;
    @BindView(R.id.tv_ry_y)
    TextView tvRyY;
    @BindView(R.id.tv_wldtbz)
    TextView tvWldtbz;
    @BindView(R.id.tv_tlzt)
    TextView tvTlzt;
    @BindView(R.id.tv_wd)
    TextView tvWd;
    @BindView(R.id.tv_xjsd)
    TextView tvXjsd;
    //@BindView(R.id.tv_cgqzt)
    //TextView tvCgqzt;
    @BindView(R.id.tv_dl_num)
    TextView tvDlNum;
    private Timer timer;

    public static MsgFeedBackOneFragment newInstance() {
        MsgFeedBackOneFragment fragment = new MsgFeedBackOneFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMsgFeedBackOneComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_msg_feed_back_one, container, false);
    }

    private int count = 1;
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    updateUi();
                    break;

                default:
                    break;
            }
        }

        ;
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }

    private void updateUi() {
        if (responseMsg != null) {
            tvXzz.setText(responseMsg.imu[2] + "");
            tvPhjd.setText(responseMsg.imu[0] + "");
            tvCqjd.setText(responseMsg.imu[1] + "");
            if (responseMsg.battery[0] < 75) {
                tvDl.setBackgroundColor(getResources().getColor(R.color.red));
                tvDlNum.setText(responseMsg.battery[0]+"");
                tvDlNum.setTextColor(getResources().getColor(R.color.red));
            } else {
                tvDl.setBackgroundColor(getResources().getColor(R.color.green));
                tvDlNum.setText(responseMsg.battery[0]+"");
                tvDlNum.setTextColor(getResources().getColor(R.color.green));
            }

            tvGh.setText(responseMsg.power_diss[0] + "");
            if (responseMsg.ctrl_mode == 0) {
                tvKzms.setText("遥控模式");
            } else if (responseMsg.ctrl_mode == 33) {
                tvKzms.setText("人员跟随模式");
            } else if (responseMsg.ctrl_mode == 66) {
                tvKzms.setText("自主机动模式");
            }

//            for (int i = 0; i < 4; i++) {
//                for (int j = 0; j < 3; j++) {
//                    if (responseMsg.sensor_diag_char[i][j] == 0) {
//                        tvCgqzt.setText("所有关节正常");
//                    } else if (responseMsg.sensor_diag_char[i][j] == 1) {
//                        tvCgqzt.setText("腿" + i + "关节" + "j" + "角度异常");
//                    } else if (responseMsg.sensor_diag_char[i][j] == 2) {
//                        tvCgqzt.setText("腿" + i + "关节" + "j" + "电流异常");
//                    }
//                }
//            }


            tvMbdX.setText(responseMsg.target_p[0] + "");
            tvMbdY.setText(responseMsg.target_p[1] + "");
            tvRyX.setText(responseMsg.leader_p[0] + "");
            tvRyY.setText(responseMsg.leader_p[1] + "");
            if (responseMsg.grid_map_flag_char == 0) {
                tvWldtbz.setText("无效");
            } else {
                tvWldtbz.setText("有效");
            }

            if (responseMsg.imu_state_char == 0) {
                tvTlzt.setText("正常");
                tvTlzt.setTextColor(getResources().getColor(R.color.green));
            } else if (responseMsg.imu_state_char == 1) {
                tvTlzt.setText("接收异常");
                tvTlzt.setTextColor(getResources().getColor(R.color.red));
            } else if (responseMsg.imu_state_char == 2) {
                tvTlzt.setText("传输中断");
                tvTlzt.setTextColor(getResources().getColor(R.color.red));
            }


            tvWd.setText(responseMsg.temperature[0] + "");
            tvXjsd.setText(responseMsg.robot_v[0] + "");
            if (responseMsg.gait_id == 12) {
                tvDqbt.setText("行走步态");
                if (responseMsg.gait_state_id == 0) {
                    tvDqbtzt.setText("闭环当前");
                } else if (responseMsg.gait_state_id == 1) {
                    tvDqbtzt.setText("初始化");
                } else if (responseMsg.gait_state_id == 2) {
                    tvDqbtzt.setText("行走");
                } else if (responseMsg.gait_state_id == 3) {
                    tvDqbtzt.setText("停止");
                }
            } else if (responseMsg.gait_id == 13) {
                tvDqbt.setText("安全模式");
                if (responseMsg.gait_state_id == 0) {
                    tvDqbtzt.setText("闭环当前");
                }
            } else if (responseMsg.gait_id == 14) {
                tvDqbt.setText("小跑步态");
                if (responseMsg.gait_state_id == 0) {
                    tvDqbtzt.setText("闭环当前");
                } else if (responseMsg.gait_state_id == 1) {
                    tvDqbtzt.setText("初始化");
                } else if (responseMsg.gait_state_id == 2) {
                    tvDqbtzt.setText("小跑");
                } else if (responseMsg.gait_state_id == 3) {
                    tvDqbtzt.setText("停止");
                } else if (responseMsg.gait_state_id == 7) {
                    tvDqbtzt.setText("腾空");
                } else if (responseMsg.gait_state_id == 8) {
                    tvDqbtzt.setText("非腾空");
                }
            } else if (responseMsg.gait_id == 15) {
                tvDqbt.setText("站立步态");
                if (responseMsg.gait_state_id == 0) {
                    tvDqbtzt.setText("闭环当前");
                } else if (responseMsg.gait_state_id == 7) {
                    tvDqbtzt.setText("站立");
                } else if (responseMsg.gait_state_id == 8) {
                    tvDqbtzt.setText("蹲坐");
                }
            } else if (responseMsg.gait_id == 16) {
                tvDqbt.setText("自恢复步态");
                if (responseMsg.gait_state_id == 0) {
                    tvDqbtzt.setText("闭环当前");
                } else if (responseMsg.gait_state_id == 7) {
                    tvDqbtzt.setText("恢复");
                } else if (responseMsg.gait_state_id == 8) {
                    tvDqbtzt.setText("站立");
                }
            } else {
                tvDqbt.setText(responseMsg.gait_id + "");
            }
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                Message message = new Message();
                message.what = 1;
                handler.sendMessage(message);

            }
        }, 1000, 3000);

    }


    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
}
